import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BuildGraph {

	private static boolean insert(Concept root, Concept c) {
		boolean flag = false;
		for (int i = 0; i < root.childcount(); i++) {
			flag = flag || insert(root.getChild(i), c);
		}
		if (flag) {
			return true;
		}
		if (root.isChild(c)) {
			root.addChild(c);
			c.addParent(root);
			return true;
		}
		return false;
	}

	private static void print(Concept c) {
		System.out.println(c.string());
		System.out.println("child count:" + c.childcount());
		System.out.println("parent count:" + c.parentcount() + "\n");
		for (int i = 0; i < c.childcount(); i++) {
			print(c.getChild(i));
		}
	}

	private static void sort(List<String> list, List<String> list2) {
		for (int j = 1; j < list.size(); j++) {
			for (int i = 0; i < (list.size() - j); i++) {
				if (list.get(i).length() < list.get(i + 1).length()) {
					String temp = list.get(i);
					list.set(i, list.get(i + 1));
					list.set(i + 1, temp);

					String temp1 = list2.get(i);
					list2.set(i, list2.get(i + 1));
					list2.set(i + 1, temp1);
				}
			}
		}
	}

	private static Concept read(String Attrname, String ObjName,
			String allattrlist) throws IOException {
		Concept root = new Concept();
		List<String> temp1 = new ArrayList<String>();
		// read attrlist.txt
		FileReader readattrlist = new FileReader(allattrlist);
		BufferedReader brattrlist = new BufferedReader(readattrlist);
		String attrlist;
		while ((attrlist = brattrlist.readLine()) != null) {
			temp1.add(attrlist);
		}
		root.addAttribute(temp1);
		// read attrlist.txt end
		FileReader reader = new FileReader(Attrname);
		BufferedReader brAttr = new BufferedReader(reader);
		FileReader readerObj = new FileReader(ObjName);
		BufferedReader brObj = new BufferedReader(readerObj);
		String a, b;

		List<String> tempAttr = new ArrayList<String>();
		List<String> tempObj = new ArrayList<String>();
		while (((a = brAttr.readLine()) != null)
				&& ((b = brObj.readLine()) != null)) {
			if (!tempAttr.contains(a)) {
				tempAttr.add(a);
				tempObj.add(b);
			}
		}
		sort(tempAttr, tempObj);
		for (int j = 0; j < tempAttr.size(); j++) {
			Concept c = new Concept();
			c.AddFormat(tempAttr.get(j), tempObj.get(j));
			insert(root, c);
		}
		
		return root;
	}

	private static Concept search(Concept root, Concept c) {
		if ((!root.NEG) && (root.isChild(c))) {
			return root;
		}
		for (int i = 0; i < root.childcount(); i++) {
			Concept t;
			if ((t = search(root.getChild(i), c)) != null) {
				return t;
			}
		}
		return null;
	}

	public static void main(String[] args) throws IOException {

		Concept root = read("lattice_test2_attr.txt", "lattice_test2_obj.txt", "attrlist.txt");
		print(root);
		root.NEG = true;
		Concept c = new Concept();
		c.AddFormat("i,m,n,h,j,c,g,e,o", "4 9");
		c = search(root, c);
		System.out.println("unit test: search");
		if (c != null) {
			System.out.println(c.string());
		}

	}

}
